<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	function __construct() {
		parent::__construct();
		session_start();
		
		$this->parent_type = strtolower(get_called_class());
		
		if (url_contains(ADMIN_FOLDER . '/admin')) {
			if (!empty($_SESSION) && $_SESSION['ucid'] != ADMIN_UCID) { show_404(); }
		}
		
		// 管理员测试
		//$_SESSION['ucid'] = ADMIN_UCID;//ADMIN_UCID
	}
	
	function _get_menu_tabs() {
		$parent_variable = $this->uri->segment(1);
		
		$condition['pid'] = 0;
		$data['parent'] = $this->type_model->search_by_condition($condition);
		
		foreach ($data['parent'] as $k => $v) {
			if ($v['variable'] == $parent_variable) {
				$data['parent'][$k]['active'] = TRUE;
			}
		}
		
		if ($_SERVER['REQUEST_URI'] == "/" OR $_SERVER['REQUEST_URI'] == "/index.php") {
			return $data;
		} else {
			$condition['pid'] = $this->type_model->_get_by('variable', $parent_variable, 'id', TRUE);
			$data['child'] = $this->type_model->search_by_condition($condition);
			return $data;
		}
	}
	
	/**
	 * 得到表单数据
	 * @param $unset 过滤数据 可以是字符串 也可以是数组
	 */
	function _get_form_array($form_fields_data, $unset = '') {
		foreach ($form_fields_data as $v) {
			if (is_array($unset)) {
				if (!in_array($v['name'], $unset)) {
					$form_data[$v['name']] =  $v['value'];
				}
			} else {
				if ($v['name'] != $unset) {
					$form_data[$v['name']] =  $v['value'];
				}
			}
		}
		return $form_data;
	}
	
	function _upload_pic($upload_path = '', $make_thumb = FALSE, $thumb_config = array()) {
		$config['upload_path'] = empty($upload_path) ? USER_UPLOAD_PIC_FOLDER . date("Y/m/d", time()) : $upload_path;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '1000';
		$config['max_width']  = '10240';
		$config['max_height']  = '7680';
		$config['encrypt_name'] = TRUE;
		$this->load->library('upload');
		
		// Bug in CodeIgniter that if extend Upload Class, it will not auto initialize
		$this->upload->initialize($config);
		
		if ($this->upload->do_upload('uploadedFile')) {
			$upload_data = $this->upload->data();
			$upload_data['file_db_path'] = '/' . $config['upload_path'] . '/' . $upload_data['file_name'];
			
			if ($make_thumb && count($thumb_config) == 2) {
				$this->_make_thumb($upload_data['full_path'], $thumb_config[0], $thumb_config[1]);
				$upload_data['thumb_db_path'] = '/' . $config['upload_path'] . '/' . $upload_data['raw_name'] . '_thumb' . $upload_data['file_ext'];
			}
			
			return $upload_data;
		} else {
			return FALSE;
		}
	}
	
	function _make_thumb($source_path, $width, $height) {
		$config['image_library'] = 'gd2';
		$config['source_image'] = $source_path;
		$config['create_thumb'] = TRUE;
		$config['maintain_ratio'] = TRUE;
		
		$config['width'] = $width;
		$config['height'] = $height;
		
		$this->load->library('image_lib', $config); 
		$this->image_lib->resize();
	}
	
	function _get_current_editable_obj($type, $selector) {
		$temp = unserialize($this->type_model->_get_by('variable', $type, 'editable_obj', TRUE));
		
		foreach ($temp as $v) {
			if (selector_replace($v['selector']) == $selector) {
				return $selector_data = $v;
			}
		}
	}
	
	function _get_max_weight($data) {
		foreach ($data as $k => $v) {
			$weight_array[$k] = $v['weight'];
		}
		sort($weight_array, SORT_NUMERIC);
		return end($weight_array);
	}
	
	function _update_weight($post_weight, $model_abbr) {
		switch ($model_abbr) {
			case 'common':
				$model = 'con_common_model';
				break;
			case 'photo':
				$model = 'con_photo_model';
				break;
			case 'team':
				$model = 'con_team_model';
				break;
		}
		
		foreach ($post_weight as $k => $v) {
			eval("\$origin[\$k] = \$this->" . $model . "->_get_by_id(\$v);");
		}
						
		// 对origin以weight升序排序
		foreach ($origin as $k => $v) {
			$weight[$k]  = $v['weight'];
		}
		array_multisort($weight, SORT_ASC, $origin);
 
		foreach ($post_weight as $k => $v) {
			$update_data['id'] = $v;
			$update_data['weight'] = $origin[$k]['weight'];
			eval("\$this->" . $model . "->update(\$update_data);");
		}
	}
	
	function _delete_item($model_abbr) {
		switch ($model_abbr) {
			case 'common':
				$model = 'con_common_model';
				break;
			case 'photo':
				$model = 'con_photo_model';
				break;
			case 'team':
				$model = 'con_team_model';
				break;
		}
		
		$update_data['id'] = $_POST['id'];
		$update_data['is_visible'] = ($_POST['is_delete'] == 'true') ? IS_DELETE : NOT_DELETE;
		eval("\$this->" . $model . "->update(\$update_data);");
	}
	
	function _get_info_by_filename($filename, $result_key_set = array(), $info_delimiter = '-') {
		if (strpbrk($filename, '.')) {
			$filename_array = explode('.', $filename);
			array_pop($filename_array);
			
			$filename_not_ext = $filename_array[0];
		} else {
			$filename_not_ext = $filename;
		}
		
		if (count($result_key_set) != 1) {
			$filename_not_ext_array = explode($info_delimiter, $filename_not_ext);
			foreach ($filename_not_ext_array as $k => $v) {
				$info[$result_key_set[$k]] = $v;
			}
		} else {
			$info[$result_key_set[0]] = $filename_not_ext;
		}
		
		return $info;
	}
}